package com.mallcai.bigdata.ladon.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * described : 压缩工具类
 * Created by DiLong on 2017-12-25 19:10.
 * Modify:
 */
public class CompressZipUtils {

    public static final int BUFFER = 2048;

    /**
     * 压缩文件
     *
     * @param sourceFile    String 等待压缩的文件路径
     * @param targetZipFile String 压缩后的文件路径 *.zip 结束
     */
    public static void compressFile(String sourceFile, String targetZipFile) throws Exception {
        if (StringUtils.isBlank(sourceFile)) {
            throw new RuntimeException("待压缩源文件路径为空!");
        }
        if (StringUtils.isBlank(targetZipFile)) {
            throw new RuntimeException("保存压缩包的文件路径为空!");
        }

        checkTargetFile(targetZipFile);

        BufferedInputStream origin;
        FileOutputStream dest = new FileOutputStream(targetZipFile);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
        out.setMethod(ZipOutputStream.DEFLATED);
        byte[] data = new byte[BUFFER];

        FileInputStream fi = new FileInputStream(new File(sourceFile));
        origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry entry = new ZipEntry(sourceFile);
        out.putNextEntry(entry);
        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1) {
            out.write(data, 0, count);
        }
        origin.close();

        out.close();
    }

    /**
     * 压缩文件
     *
     * @param sourceFiles   List 等待压缩的文件路径列表
     * @param targetZipFile String 压缩后的文件路径 *.zip 结束
     */
    public static void compressFiles(List<String> sourceFiles, String targetZipFile) throws Exception {
        if (sourceFiles == null || sourceFiles.isEmpty()) {
            throw new RuntimeException("待压缩源文件路径为空!");
        }
        if (StringUtils.isBlank(targetZipFile)) {
            throw new RuntimeException("保存压缩包的文件路径为空!");
        }

        checkTargetFile(targetZipFile);

        BufferedInputStream origin;
        FileOutputStream dest = new FileOutputStream(targetZipFile);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
        out.setMethod(ZipOutputStream.DEFLATED);
        byte[] data = new byte[BUFFER];

        for (String sourceFile : sourceFiles) {
            FileInputStream fi = new FileInputStream(new File(sourceFile));
            origin = new BufferedInputStream(fi, BUFFER);
            ZipEntry entry = new ZipEntry(sourceFile);
            out.putNextEntry(entry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
        }
        out.close();
    }

    private static void checkTargetFile(String targetZipFile) {
        try {
            File targetFile = new File(targetZipFile);
            if (targetFile.exists()) {
                targetFile.delete();
            }
        } catch (Exception e) {
            throw new RuntimeException("删除已经存在的目标文件失败!");
        }
    }
}
